package com.z.common.gen.web;

import com.github.pagehelper.PageInfo;
import com.z.common.gen.entity.SysDict;
import com.z.common.gen.service.ISysDictService;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * 数据字典Controller
 *
 * @author z
 * @date 2022-09-21
 */
@RestController
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {

    private String prefix = "system/dict";

    @Autowired
    private ISysDictService sysDictService;

    @GetMapping("/main")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询数据字典列表
     */
    @ResponseBody
    @GetMapping("/data")
    public Result list(@ModelAttribute SysDict sysDict, PageVo pageVo) {
        PageInfo<SysDict> pageInfo = sysDictService.selectSysDictPage(sysDict, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增数据字典
     */
    @GetMapping("/add")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增数据字典
     */
    @ResponseBody
    @PostMapping("/save")
    public Result save(@RequestBody SysDict sysDict) {
        return Result.decide(sysDictService.save(sysDict));
    }

    /**
     * 修改数据字典
     */
    @GetMapping("/edit")
    public ModelAndView edit(String id, ModelMap map) {
        SysDict sysDict =sysDictService.getById(id);
        map.put("sysDict", sysDict);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改数据字典
     */
    @ResponseBody
    @PutMapping("/update")
    public Result update(@RequestBody SysDict sysDict) {
        return Result.decide(sysDictService.updateById(sysDict));
    }

    /**
     * 删除数据字典
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    public Result batchRemove(String ids) {
        return Result.decide(sysDictService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除数据字典
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") String id) {
        return Result.decide(sysDictService.removeById(id));
    }
}
